//定义一个入口函数，加载findAttend()函数，以避免页面还没加载完参数，就执行js代码
$(function(){  
	//展示第一页为首页，其他页码就在该基础上加1
	findAttend(1);
	//首页
	$("#firstPage").click(function(){
//		alert("666");
		var currentpage = parseInt($("#pageIndex").html());
		if(currentpage!=1){
			findAttend(1);
		}
	});
	//下一页操作
	$("#nextPage").click(function(){
		var currentpage = parseInt($("#pageIndex").html());
		var totalpage = parseInt($("#totalPage").html());
		if(currentpage < totalpage){
			findAttend(currentpage+1);
		}
	});
	//上一页操作
	$("#prePage").click(function(){
		var currentpage = parseInt($("#pageIndex").html());
		if(currentpage > 1){
			findAttend(currentpage-1);
		}
	});
	//尾页
	$("#endPage").click(function(){
		var currentpage = parseInt($("#pageIndex").html());
		var totalpage = parseInt($("#totalPage").html());
		if(currentpage != totalpage){
			findAttend(totalpage);
		}
	});
		
})

// 条件查询,传入当前页page
function findAttend(page) {

	var reg = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/;
	
	var signInStr = $("#signInStr").val();
	var signOutStr = $("#signOutStr").val();
	
	if (signInStr != '' && !reg.test(signInStr)) {
		alert("输入的签到时期格式有误，正确格式为：YYYY-MM-DD");
		return;
	}
	if (signOutStr != '' && !reg.test(signOutStr)) {
		alert("输入的签退时期格式有误，正确格式为：YYYY-MM-DD");
		return;
	}
	if (signInStr != '' && signOutStr != '') {
		var signIn = new Date(signInStr);
		var signOut = new Date(signOutStr);
		if (signIn.getTime() > signOut.getTime()) {
			alert("签退日期不能早于签到日期");
			return;
		}
	}
	
	//分页查询
	$.ajax({
		url:"/Attendance/showAttendancesByPage",
		type:"post",
		data:{
			pageIndex:page,
			startDate:signInStr,
			endDate:signOutStr
		},
		dataType:"json",
		success:function(data){
			
			var content="";
			var attendances = data.beanList;
			var totalRecord = data.totalRecord;
			var pageIndex = data.pageIndex;
			var totalPage = data.totalPage;
//			content+='<table class="table table-bordered" style="margin-top: 20px;" >'
			content+='<thead>'
				+'<tr>'
				+'<th style="width: 150px;">姓名</th>'
				+'<th>考勤日期</th>'
				+'<th>签到时间</th>'
				+'<th>签退时间</th>'
				+'<th>IP</th>'
				+'</tr>'
				+'</thead>';
			
			for(var i=0;i<attendances.length;i++){
				var po = attendances[i];
					
				content+='<tbody>'
					+'<tr class="success">'
					+'<td>'+po.am_username+'</td>'
					+'<td>'+po.am_date+'</td>'
					+'<td>'+po.am_signtime+'</td>'
					+'<td>'+po.am_signbacktime+'</td>'
					+'<td>'+po.am_userip+'</td>'
					+'</tr>'
					+'</tbody>'	
			}
			
//			content+='</table>'
//				for(var i=data.pageBegin; i<data.pageEnd;i++){
//					if(data.pageIndex==i){
//						content+='['+i+']';
//					}else if(data.pageIndex!=i){
//						content+='<a href="javascript:findAttend('+i+')">['+i+']</a>';
//					}
//				}
//			content+='</br><span>共  <span>'+data.totalRecord+'</span> 条记录    当前第  <span id="pageIndex">'+data.pageIndex+'</span> 页     共   <span id="totalPage">'+data.totalPage+'</span> 页</span>'
//				+'<ul class="pagination" style="float: right;margin: 0;">'
//				+'<li><span id="firstPage">首页</span></li>'
//				+'<li><span id="prePage">上一页</span></li>'
//				+'<li><span id="nextPage">下一页</span></li>'
//				+'<li><span id="endPage">尾页</span></li>'
//				+'</ul>'
			$("#attendanceInfo").html(content);
			$("#totalRecord").html(totalRecord);
			$("#pageIndex").html(page);
			$("#totalPage").html(totalPage);
            
		},
		error:function(data){
			alert("出错了");
		}
	});
}

/**
 * 签到
 * @param path
 */
function attendSignIn() {
	$.ajax({
		url: "/Attendance/signIn",
		type: "POST",
		dataType: "TEXT",
		success: function(data) {
			if (data == 2) {
				alert("今天已经签到过了，不可重复签到！");
			} else if (data == 0) {
				alert("签到失败！");
			} else if (data == -2){
				alert("签到成功，当前考勤状态为迟到");
			}else if (data == -3){
				alert("签到失败，当前状态为缺勤");
			}else {
				alert("签到成功！");
				//在页面显示当前用户信息
				showAttendance();
			}
		}, error: function(data) {
			alert("系统错误，请联系管理员！");
		}
	});
	
	/**
	 * 当签到成功后，页面展示考勤信息
	 */
	function showAttendance(){
		$.ajax({
			url:"/Attendance/showAttendance",
			type:"post",
			dataType:"json",
			success:function(data){
				var content="";
				content+='<table class="table table-bordered" style="margin-top: 20px;" >'
					+'<thead>'
					+'<tr>'
					+'<th style="width: 150px;">姓名</th>'
					+'<th>考勤日期</th>'
					+'<th>签到时间</th>'
					+'<th>签退时间</th>'
					+'<th>IP</th>'
					+'</tr>'
					+'</thead>'
					+'<tr class="success">'
					+'<td>'+data.am_username+'</td>'
					+'<td>'+data.am_date+'</td>'
					+'<td>'+data.am_signtime+'</td>'
					+'<td>'+data.am_signbacktime+'</td>'
					+'<td>'+data.am_userip+'</td>'
					+'</tr>'
					+'</table>';
				$("#attendanceInfo").html(content);
			},
			error:function(data){
				alert("出错了");
			}
			
		});
	}
	//showAttendance()方法结束
}

/**
 * 签退
 * @param path
 */
function attendSignOut() {
	$.ajax({
		url: "/Attendance/signBack",
		type: "POST",
		dateType: "TEXT",
		success: function(data) {
			if (data == -2) {
				alert("您还没有签到，请先签到");
			} else if (data == 0) {
				alert("签退失败！");
			} else if(data == -3){
				alert("还未到签退时间,请在下班时间后签到");
			} else {
				alert("签退成功！");
				showAttendance();
			}
		}, error: function(data) {
			alert("系统错误，请联系管理员！");
		}
	});
	
	/**
	 * 当签退成功后，页面展示考勤信息
	 * @returns
	 */
	function showAttendance(){
		$.ajax({
			url:"/Attendance/showAttendance",
			type:"post",
			dataType:"json",
			success:function(data){
				var content="";
				content+='<table class="table table-bordered" style="margin-top: 20px;" >'
					+'<thead>'
					+'<tr>'
					+'<th style="width: 150px;">姓名</th>'
					+'<th>考勤日期</th>'
					+'<th>签到时间</th>'
					+'<th>签退时间</th>'
					+'<th>IP</th>'
					+'</tr>'
					+'</thead>'
					+'<tr class="success">'
					+'<td>'+data.am_username+'</td>'
					+'<td>'+data.am_date+'</td>'
					+'<td>'+data.am_signtime+'</td>'
					+'<td>'+data.am_signbacktime+'</td>'
					+'<td>'+data.am_userip+'</td>'
					+'</tr>'
					+'</table>'
					+'<span>共1条记录 当前第1页  共1页</span>'
				$("#attendanceInfo").html(content);
			},
			error:function(data){
				alert("出错了");
			}
			
		});
	}
	//showAttendance()方法结束
	
}

